package com.wang.lock;

public class WaitThread1 {

    public static void main(String[] args) {
        A a = new A();
        ThreadA threadA = new ThreadA(a);
        ThreadB threadB = new ThreadB(a);
            threadA.start();
            threadB.start();
    }
}

class A {
    int num ;

    volatile boolean flag = false;

    public void setNum(int num) {
        this.num = num;
    }

    public synchronized void medA(){
        System.out.println("MED_A");
        try {
            for(int i=1;i<100;i+=2){
                while(!flag){
                    wait();
                }
                flag = !flag;
                System.out.println(Thread.currentThread().getName()+"::"+i);
                notify();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void medB(){
        System.out.println("MED_B");
        try {
            for(int i = 0;i<100;i=i+2){

                while (flag){
                    wait();
                }
                flag = !flag;
                System.out.println(Thread.currentThread().getName()+"::"+i);
                notify();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class ThreadA extends Thread{

    A a ;
    ThreadA(A a){
        this.a = a;
    }

    @Override
    public void run() {
        a.medA();
    }
}

class ThreadB extends Thread{

    A a ;
    ThreadB(A a){
        this.a = a;
    }

    @Override
    public void run() {
        a.medB();
    }
}